package com.gzq.common;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Adapter<T> extends BaseAdapter {

    private Context context;
    private List<T> data;
    private OnGetView<T> onGetView;
    private OnGetLayoutType<T> onGetLayoutType;
    private Map<Integer, Integer> layoutTypeMap;

    Adapter(Context context) {
        this.context = context;
        data = new ArrayList<>();
        layoutTypeMap = new HashMap<>();
    }

    @Override
    public int getViewTypeCount() {
        if (layoutTypeMap.size() <= 0) {
            return 0;
        }
        return layoutTypeMap.size();
    }

    @Override
    public int getItemViewType(int position) {
        return onGetLayoutType.getLayoutType(position, this.data);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        int type = getItemViewType(position);
        ViewHolder viewHolder = null;
        //因为考虑到item布局重用的情况，优化ListView
        if (convertView == null) {
            convertView = LayoutInflater.from(context)
                    .inflate(layoutTypeMap.get(type), parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (onGetView != null) {
            onGetView.getView(viewHolder, position, this.data);
        }
        return viewHolder.getConvertView();
    }

    //获取数据
    public List<T> getData() {
        return this.data;
    }

    //设置数据
    public void setData(List<T> data) {
        if (data == null) {
            data = new ArrayList<T>();
        }
        this.data = data;
        this.notifyDataSetChanged();
    }

    //更新数据
    public void updateData(List<T> data) {
        if (data == null) {
            data = new ArrayList<T>();
        }
        this.data = data;
        this.notifyDataSetChanged();
    }

    //添加数据
    public void addData(List<T> data) {
        if (this.data == null) {
            data = new ArrayList<T>();
        }
        this.data.addAll(data);
        this.notifyDataSetChanged();
    }

    //局部更新数据
    public void updateData(ListView listView, int position, T data) {
        if (data != null) {
            if (this.data == null) {
                this.data = new ArrayList<T>();
            }
            this.data.set(position, data);

            //获得第一条数据
            int firstVisiblePosition = listView.getFirstVisiblePosition();
            //获取最后一条数据
            int lastVisiblePosition = listView.getLastVisiblePosition();
            if (position >= firstVisiblePosition && position <= lastVisiblePosition) {
                /**获取指定位置view对象 ，如果存在头部布局，所以减去头布局**/
                View view = listView.getChildAt(position - firstVisiblePosition);
                getView(position, view, listView);
            }
        }
    }

    //添加布局--两者都不能为空
    public void addLayout(int type, int layoutId) {
        layoutTypeMap.put(type, layoutId);
    }

    public void setOnGetView(OnGetView<T> onGetView) {
        this.onGetView = onGetView;
    }

    //新建一个接口-把给用户的操作集中到一块，给用户操作，采用java的命令模式，可自行学习
    public interface OnGetView<T> {

        void getView(ViewHolder viewHolder, final int position, List<T> data);
    }

    //用于返回指定布局的类型
    public void setOnGetLayoutType(OnGetLayoutType<T> onGetLayoutType) {
        this.onGetLayoutType = onGetLayoutType;
    }

    public interface OnGetLayoutType<T> {

        int getLayoutType(int postion, List<T> data);
    }

    //封装一个万能的ViewHolder
    static class ViewHolder {

        //用于存储控件
        private SparseArray<View> views;
        private View convertView;

        public ViewHolder(View convertView) {
            this.convertView = convertView;
            views = new SparseArray();
        }

        //获得控件
        public <T extends View> T getView(int viewId) {
            View view = views.get(viewId);
            if (view == null) {
                view = convertView.findViewById(viewId);
                views.put(viewId, view);
            }
            return (T) view;
        }

        //获得布局
        public View getConvertView() {
            return convertView;
        }
    }
}